package com.design.twords.json;

import android.util.Log;

import com.design.twords.database.Phrase_all;
import com.design.twords.database.RelWord_all;
import com.design.twords.database.Sentence_all;
import com.design.twords.database.Translation_all;
import com.design.twords.database.Word_all;
import com.design.twords.entity.JsonWordDetail;
import com.design.twords.entity.PhraseContent;
import com.design.twords.entity.SentenceContent;
import com.design.twords.entity.Tran;
import com.design.twords.entity.relWordContent;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.litepal.LitePal;

import java.util.List;

/**
 * Create by Alen
 * Time:  23:40
 */
public class JsonForDatabase {
    private static final String TAG = "JsonForDatabase_1";


    //解析Json 并保存到数据库中
    public static boolean analyseJson(String json, String type) {
        int i = 0;

        Gson gson = new Gson();
        List<JsonWordDetail> wordDetails = gson.fromJson(json, new TypeToken<List<JsonWordDetail>>() {}.getType());
        for (JsonWordDetail detail : wordDetails) {
            i++;
            if(i >= 51)break;
            Word_all word_all = new Word_all();
            //设置单词id
            word_all.setWordId(detail.getWordRank());
            //设置归属书籍
            word_all.setWordBook(type);
            //设置单词
            word_all.setWord(detail.getHeadWord());

            //设置英音标
            if(detail.getContent().getWord().getContent().getUkphone() != null){
                if (detail.getContent().getWord().getContent().getUkphone().contains(";")){
                    String[] split = detail.getContent().getWord().getContent().getUkphone().split(";");
                    word_all.setUkPhone("["+split[0]+"]");
                }else {
                    word_all.setUkPhone("["+detail.getContent().getWord().getContent().getUkphone()+"]");
                }
            }

            //设置美音标
            if(detail.getContent().getWord().getContent().getUsphone() != null){
                if (detail.getContent().getWord().getContent().getUsphone().contains(";")){
                    String[] split = detail.getContent().getWord().getContent().getUsphone().split(";");
                    word_all.setUkPhone("["+split[0]+"]");
                }else {
                    word_all.setUsPhone("["+detail.getContent().getWord().getContent().getUsphone()+"]");
                }

            }

            //设置巧计方法
            if(detail.getContent().getWord().getContent().getRemMethod() != null){
                word_all.setRemMethod(detail.getContent().getWord().getContent().getRemMethod().getVal());
            }
            //保存
            word_all.save();


            //设置句子
            if(detail.getContent().getWord().getContent().getSentence() != null){
                List<SentenceContent.Sentences> sentences = detail.getContent().getWord().getContent().getSentence().getSentences();
                for(SentenceContent.Sentences sc : sentences){
                    Sentence_all sentence = new Sentence_all();
                    sentence.setWordId(detail.getWordRank());
                    sentence.setWordBook(type);
                    sentence.setSentence(sc.getsContent());
                    sentence.setTran(sc.getsCn());
                    sentence.save();

                }
            }

            //设置同根词
            if (detail.getContent().getWord().getContent().getRelWord() != null){
                List<relWordContent.relWod> rels = detail.getContent().getWord().getContent().getRelWord().getRels();
                for (relWordContent.relWod rw : rels){
                    List<relWordContent.relWod.relWords> words = rw.getWords();
                    for (relWordContent.relWod.relWords rww: words){
                        RelWord_all relWord = new RelWord_all();
                        relWord.setWordId(detail.getWordRank());
                        relWord.setWordBook(type);
                        relWord.setWrodType(rw.getPos());
                        relWord.setWord(rww.getHwd());
                        relWord.setTran(rww.getTran());
                        relWord.save();
                    }
                }
            }

            //设置短语
            if(detail.getContent().getWord().getContent().getPhrase() != null){
                List<PhraseContent.Phrases> phrases = detail.getContent().getWord().getContent().getPhrase().getPhrases();
                for (PhraseContent.Phrases ph : phrases){
                    Phrase_all phrase = new Phrase_all();
                    phrase.setWordId(detail.getWordRank());
                    phrase.setWordBook(type);
                    phrase.setPhrase(ph.getpContent());
                    phrase.setTrans(ph.getpCn());
                    phrase.save();
                }
            }

            //设置翻译
            if(detail.getContent().getWord().getContent() != null){
                List<Tran> trans = detail.getContent().getWord().getContent().getTrans();
                for (Tran tr : trans){
                    Translation_all translation = new Translation_all();
                    translation.setWordId(detail.getWordRank());
                    translation.setWordBook(type);
                    translation.setWordType(tr.getPos());
                    translation.setCHSMeaning(tr.getTranCn());
                    translation.setENMeaning(tr.getTranOther());
                    translation.save();
                }
            }

        }

        List<Word_all> all = LitePal.findAll(Word_all.class);
        if (all.size() > 0){
            Log.i(TAG, "Word_all:"+all.size());
            Log.i(TAG, "finish");

            return true;
        }

        Log.i(TAG, "false");
        return false;

    }

}